<?php
/**
 * V6
 *
 */
class data_invitation_public extends data_invitation {
	public function __construct() {
		parent::__construct();
		$this->expire_days = 6;			// public code的过期时间，天数
	}
	/**
	 * 获得一个邀请码，如果一天之内生成过邀请码，则返回该邀请码，如果没有，则生成新的邀请码
	 * 		检查circle权限
	 *
	 * @param int $uid
	 * @param int $circle_id
	 * @return Array
	 */
	public function get_code ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			// 检查circle权限
			$data_circle = new data_circle();
			if ($data_circle->is_member($uid, $circle_id)) {
				$expire_date = date('Y-m-d H:i:s', (time()-86400));		// 一天之内生成的code
				// 查询已有的未过期的code
				$execute = $this->db->execute('
					select
						*
					from
						invitation_public
					where
						inviter_id		= "'.$uid.'"		and
						circle_id		= "'.$circle_id.'"	and
						dt				> "'.$expire_date.'"
					order by
						id DESC
					limit
						1
				');
				if ($execute) {
					return $execute[0]['code'];
				} else {
					$code 	= $this->make_code('public', $uid, $circle_id);
					$insert_id = $this->db->execute('
						insert into
							invitation_public
						set
							inviter_id		= "'.$uid.'",
							circle_id		= "'.$circle_id.'",
							code			= "'.$code.'",
							dt				= "'.date('Y-m-d H:i:s').'"
					');
					if ($insert_id) {
						return $code;
					} else return false;
				}
			} else return false;
		} else return false;
	}
	/**
	 * 通过邀请码返回邀请者的id和圈子id
	 * 		只有一条记录
	 * 		检查了邀请者的权限
	 *
	 * @param String $code
	 * @return Array
	 */
	public function decode ($code) {
		if ($this->is_code($code) == 'public') {
			$execute = $this->db->execute('
				select 
					*
				from
					invitation_public
				where
					code = "'.$code.'"
				limit
					1
			');
			if ($execute) {
				// 检查权限
				$data_circle = new data_circle();
				if ($data_circle->is_member($execute[0]['inviter_id'], $execute[0]['circle_id'])) {
					return $execute[0];
				} else {
					// 邀请者已经不再是圈子成员了，删除此条记录
					$this->db->execute('
						delete from
							invitation_public
						where
							id	= "'.$execute[0]['id'].'"
					');
					return false;
				}
			} else return false;
		} else return false;
	}
	
	public function accept ($decode, $uid) {
		$uid = intval($uid);
		
		if (is_array($decode) and $uid) {
			// 发布Event：加入圈子、加为好友，发布notice
			$data_event = new data_event();
			$event_param = array (
				'user_id'		=> $uid,
				'inviter_id'	=> $decode['inviter_id'],
				'circle_id'		=> $decode['circle_id'],
			);
			$data_event->add('invitation_accept', $event_param, 'high');
			
			return $uid;
		} else return false;
	}
	
	/**
	 * 清理掉所有过期的邀请
	 *
	 * @param int $expire_days
	 * @param string $accepter_type		// public / email / weibo / all	(默认为public)
	 * @return bool
	 */
	public function clear_expire_code ($expire_days=6) {
		$expire_days = intval($expire_days);
		
		if ($expire_days) {
			$this->db->execute('
				delete from
					invitation_public
				where
					dt	< "'.date('Y-m-d H:i:s', (time() - ($expire_days * 86400))).'"
			');
			return true;
		} else return false;
	}

}
?>